package org.i_plus;

import java.io.File;
import java.io.IOException;


import org.i_plus.model.ConstellationBean;
import org.i_plus.model.dao.ConstellationHibernateDAO;
import org.i_plus.model.services.ConstellationService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDaily {
	private ConstellationService service ;
	public void setConstellationService(ConstellationService service){
		this.service=service;
	}
	private ConstellationHibernateDAO dao;
	public TestDaily(ConstellationHibernateDAO dao){
		this.dao=dao;
	}
	
	
	public void scratchdata() throws Exception {

		for (int a = 1; a < 13; a++) {
			String webrul = "http://tw.xingbar.com/cgi-bin/v5starfate2?fate=1&type="
					+ a;
//		String webrul = "http://tw.xingbar.com/cgi-bin/v5starfate2?fate=1&type=1";
			Document doc = Jsoup.connect(webrul).timeout(10000).get();

			String[] title = doc.select("title").get(0).text().trim()
					.split("-");
			String constellName = title[1];
			ConstellationBean beans = dao.select(constellName);
//			System.out.println("beans"+beans);
			System.out.println(constellName);
			
			
			// 心情 財運 健康 開運
			Element tempL = doc.select("div.dotL").get(0);
//			for (int i = 1; i <= 4; i++) {
//				String dotL = tempL.select("div").get(i).text().trim();
//				System.out.println(dotL);
//			}
			beans.setMind_star(tempL.select("div").get(1).text().trim());
//			System.out.println("mstar"+tempL.select("div").get(1).text().trim());
			beans.setMoney_star(tempL.select("div").get(2).text().trim());
			beans.setHealth_star(tempL.select("div").get(3).text().trim());
			beans.setLuck_dir(tempL.select("div").get(4).text().trim());
			

			// 愛情 工作 顏色 幸運物
			Element tempR = doc.select("div.dotR").get(0);
//			for (int j = 1; j <= 4; j++) {
//				String dotR = tempR.select("div").get(j).text().trim();
//				System.out.println(dotR);
//			}
			beans.setLove_star(tempR.select("div").get(1).text().trim());
//			System.out.println("Lstar"+tempR.select("div").get(1).text().trim());
			beans.setWork_star(tempR.select("div").get(2).text().trim());
			beans.setLuck_color(tempR.select("div").get(3).text().trim());
			beans.setLuck_thing(tempR.select("div").get(4).text().trim());

			// 心情 愛情 財運 工作 文章
			Element temptable = doc.select("div.textbox").get(0);
//			for (int n = 0; n < 4; n++) {
//				String td = temptable.select("td").get(2 * n + 1).text().trim();
//				System.out.println(td);
//			}
			beans.setMind_word(temptable.select("td").get(1).text().trim());
//			System.out.println("mword"+temptable.select("td").get(1).text().trim());
			beans.setLove_word(temptable.select("td").get(3).text().trim());
			beans.setMoney_word(temptable.select("td").get(5).text().trim());
			beans.setWork_word(temptable.select("td").get(7).text().trim());

			service.update(beans);
			System.out.println("beans"+beans);
			System.out.println("done");
			System.out.println("");
			

		}
		
	}

	public static void main(String[] args) throws Exception {
		ApplicationContext context =
				new ClassPathXmlApplicationContext("beans.config.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		TestDaily daily1 = (TestDaily) context.getBean("testDaily");
		daily1.scratchdata();
		session.getTransaction().commit();
		System.out.println("done");
	}
}
